import Vue from "vue";
import VueRouter from "vue-router";
import LoginView from "@/views/user/LoginView";
import RegisterView from "@/views/user/RegisterView";
import ClueEnterView from "@/views/clue/ClueEnterView";
import ClueViewView from "@/views/clue/ClueViewView";
import UserInfoView from "@/views/user/UserInfoView";
import HomeView from "@/views/Home";
import AddSchoolView from "@/views/school/AddSchoolView";
import ViewSchoolView from "@/views/school/ViewSchoolView";
import PersonalInfoView from "@/views/user/PersonalInfoView";
import CityManageView from "@/views/city/CityManageView";
import ProvinceManageView from "@/views/city/ProvinceManageView";
import ClueStatisticsView from "@/views/clue/ClueStatisticsView";
import store from "@/store/index";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
  },
  {
    path: "/user/login",
    name: "user-login",
    component: LoginView,
    meta: {
      requireAuth: false,
    },
  },
  {
    path: "/user/register",
    name: "user-register",
    component: RegisterView,
    meta: {
      requireAuth: false,
    },
  },
  {
    path: "/clue/enter",
    name: "clue-enter",
    component: ClueEnterView,
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/clue/view",
    name: "clue-view",
    component: ClueViewView,
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/user/info/view",
    name: "user-info-view",
    component: UserInfoView,
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/school/add",
    name: "add-school-view",
    component: AddSchoolView,
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/school/view",
    name: "view-school-view",
    component: ViewSchoolView,
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/city/city/manage",
    name: "city-manage-view",
    component: CityManageView,
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/city/province/manage",
    name: "province-manage-view",
    component: ProvinceManageView,
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/user/personal/info",
    name: "personal-info-view",
    component: PersonalInfoView,
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/clue/statistics",
    name: "clue-statistics",
    component: ClueStatisticsView,
    meta: {
      requireAuth: true,
    },
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path === from.path) {
    // 检测到冗余导航，不进行导航操作
    return;
  }
  store.commit("changeContentShow", false);
  setTimeout(function () {
    if (to.meta.requireAuth && !store.state.user.isLogin) {
      next({ name: "user-login" });
    }
    // 跳转到默认页面
    next();
    store.commit("changeContentShow", true);
  }, 500);
});

export default router;
